17. Write Your Own ViewModel Test
Now that you've seen how to write a test, write one on your own!
In this step, using the skills you've learned, you'll practice writing another TasksViewModel test.
Step 1: Write your own ViewModel test
You'll writegetTasksAddViewVisible(). This test should check that if you've set your filter type to show all tasks, that the Add task button is visible.
- Using for
addNewTask_setsNewTaskEvent()for reference, write a test inTasksViewModelTestcalledsetFilterAllTasks_tasksAddViewVisible()that:- Sets the filtering mode to
ALL_TASKS - Assets that the
tasksAddViewVisibleLiveDatais true
- Sets the filtering mode to
Here's some code you can use to get started:
TasksViewModelTest
@Test
fun setFilterAllTasks_tasksAddViewVisible() {
// Given a fresh ViewModel
// When the filter type is ALL_TASKS
// Then the "Add task" action is visible
}
Note:
- The
TasksFilterTypeenum for all tasks isALL_TASKS - The visibility of the button to add a task is controlled by the
LiveDatatasksAddViewVisible.
- Once you're done, run your test.